/*
 * @lc app=leetcode.cn id=606 lang=cpp
 *
 * [606] 根据二叉树创建字符串
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>

using namespace std;
// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    string ret;
    void preOrder(TreeNode* root) {
        if(!root) {
            return ;
        }
        ret += to_string(root->val);
        if(root->left) {
            ret += "(";
            preOrder(root->left);
            ret += ")";
        }
        if(root->right) {
            if(!root->left) {
                ret += "()";
            }
            ret += "(";
            preOrder(root->right);
            ret += ")";
        }
        
    }
    string tree2str(TreeNode* root) {
        preOrder(root);
        return ret;
    }
};
// @lc code=end

